#include "VDirData.h"
#include "VFunctions.h"
#include <QDir>
#include <QFileInfo>
#include <QDebug>

VDirData::VDirData(QString path)
{
    _path = path;
    load();
}

//QStringList VDirData::search(QString name, QString suffixs, char mode)
//{
//    QStringList result;
//    foreach(QString path, _list)
//    {
//        QString source = QFileInfo(path).fileName();
//        if(mode == 'a' && source == name)
//        {
//            result.append(path);
//        }
//        else if(mode == 'p' && VFunctions::matching_string(source, name))
//        {
//            if(suffixs.indexOf("("+QFileInfo(path).suffix()+")")!= -1)
//            {
//                result.append(path);
//            }
//        }
//    }

//    return result;
//}

QList<QFileInfo> VDirData::search(VFileData::VHeadFile file, QString suffixs, char mode, bool folder)
{
//    qDebug() << "source path:" << _path;
    QList<QFileInfo> result;
    QDir dir;
    dir.setCurrent(_path);
    if(dir.cd(file.path) == false)
    {
        return result;
    }

    foreach(QFileInfo mfi ,dir.entryInfoList())
    {
        if(mfi.isFile())
        {
            if(suffixs.indexOf("("+mfi.suffix()+")") == -1)
            {
                continue;
            }

            switch (mode) {
            case 'a':
                if(mfi.fileName() == file.name)
                {
                    result.append(QFileInfo(mfi.absoluteFilePath()));
                }
                break;
            case 'p':
                if(VFunctions::matching_string(mfi.fileName(), file.name) || file.name.isEmpty())
                {
                    result.append(QFileInfo(mfi.absoluteFilePath()));
                }
                break;
            }
        }
        else
        {
            if(folder == false) continue;
            if(mfi.fileName()=="." || mfi.fileName() == "..")continue;
            switch (mode) {
            case 'a':
                if(mfi.fileName() == file.name)
                {
                    result.append(QFileInfo(mfi.absoluteFilePath()));
                }
                break;
            case 'p':
                if(VFunctions::matching_string(mfi.fileName(), file.name) || file.name.isEmpty())
                {
                    result.append(QFileInfo(mfi.absoluteFilePath()));
                }
                break;
            }
        }
    }

//    QString path = dir.absolutePath() + "/" + file.name;
//    if(QFile(path).exists())
//    {
//        result.append(path);
//    }
//    if(result.count() > 0)
//    {
//        qDebug() << "count:" << result.count();
//    }
    return result;
}

void VDirData::load()
{
    _last_time = QFileInfo(_path).lastModified().toTime_t();
    QDir dir(_path);
    foreach(QFileInfo mfi ,dir.entryInfoList())
    {
        if(mfi.isFile())
        {
            _list.append(mfi.filePath());
        }
    }
}

void VDirData::reload()
{
    if(QFileInfo(_path).lastModified().toTime_t() > _last_time)
    {
        load();
    }
}
